package leetcode_100;

import java.util.ArrayList;
import java.util.List;

/**
 *@author 周杨
 *TextJustification_68 Given an array of words and a width maxWidth, format the text such that each line has exactly maxWidth characters and is fully (left and right) justified.

You should pack your words in a greedy approach; that is, pack as many words as you can in each line. Pad extra spaces ' ' when necessary so that each line has exactly maxWidth characters.

Extra spaces between words should be distributed as evenly as possible. If the number of spaces on a line do not divide evenly between words, the empty slots on the left will be assigned more spaces than the slots on the right.

For the last line of text, it should be left justified and no extra space is inserted between words.

Note:

A word is defined as a character sequence consisting of non-space characters only.
Each word's length is guaranteed to be greater than 0 and not exceed maxWidth.
The input array words contains at least one word.
Example 1:

Input:
words = ["This", "is", "an", "example", "of", "text", "justification."]
maxWidth = 16
Output:
[
   "This    is    an",
   "example  of text",
   "justification.  "
]
Example 2:

Input:
words = ["What","must","be","acknowledgment","shall","be"]
maxWidth = 16
Output:
[
  "What   must   be",
  "acknowledgment  ",
  "shall be        "
]
Explanation: Note that the last line is "shall be    " instead of "shall     be",
             because the last line must be left-justified instead of fully-justified.
             Note that the second line is also left-justified becase it contains only one word.
Example 3:

Input:
words = ["Science","is","what","we","understand","well","enough","to","explain",
         "to","a","computer.","Art","is","everything","else","we","do"]
maxWidth = 20
Output:
[
  "Science  is  what we",
  "understand      well",
  "enough to explain to",
  "a  computer.  Art is",
  "everything  else  we",
  "do                  "
]
 *describe: 文本格式校验 边界值判断  AC 99%
 *2018年5月12日 下午3:39:32
 */
public class TextJustification_68 {
	List<String> res;
	public static void main(String[] args) {
		TextJustification_68 test=new TextJustification_68();
		String words[]=new String[] {"This", "is", "an", "example", "of", "text", "justification."};
		String words1[]=new String[] {"What","must","be","acknowledgment","shall","be"};
		String words2[]=new String[] {"Science","is","what","we","understand","well","enough","to","explain",
		         "to","a","computer.","Art","is","everything","else","we","do"};
		String words3[]=new String[] {"ask","not","what","your","country","can","do",
				"for","you","ask","what","you","can","do","for","your","country"};
		test.fullJustify(words3, 16);

	}
	
	public List<String> fullJustify(String[] words, int maxWidth) {
		this.res=new ArrayList<String>();	
		int index=0,maxLength=words.length;
		while(index<maxLength) {
			StringBuilder sb=new StringBuilder();
			int start=index,end=index+1,wordsCount=1,wordsLength=words[start].length();
			while(end<maxLength) {
				int nowLength=wordsLength+words[end].length();
				if(nowLength+wordsCount<=maxWidth) {
					wordsLength=nowLength;
					++wordsCount;
				}
				else {
					--end;
					break;
				}
				++end;
			}
			if(end==maxLength) {//最后一行
				end--;
				for(int i=start;i<end;++i) {
					sb.append(words[i]);
					sb.append(" ");
				}
				sb.append(words[end]);
				for(int i=sb.length();i<maxWidth;++i) {
					sb.append(" ");
				}
				this.res.add(sb.toString());
				return this.res;
			}
			else {
				if(wordsCount==1) {
					sb.append(words[start]);
					for(int i=sb.length();i<maxWidth;++i) {
						sb.append(" ");
					}
					this.res.add(sb.toString());
				}
				else {
					int spaceAve=(maxWidth-wordsLength)/(wordsCount-1);//平均每次填入的空格
					int maxCount=(maxWidth-wordsLength)%(wordsCount-1);//有几个是最大填入空格
					StringBuilder spaceBuilder=new StringBuilder();//最大空格
					for(int i=0;i<spaceAve;++i)
						spaceBuilder.append(" ");
					for(int i=start;i<end;++i) {
						sb.append(words[i]);
						if(maxCount!=0) {
							sb.append(spaceBuilder);
							sb.append(" ");
							--maxCount;
						}
						else {
							sb.append(spaceBuilder);
						}
					}
					int nowLength=sb.length()+words[end].length();
					if(nowLength>maxWidth) {
						int delete=nowLength-maxWidth;//要删除的个数
						sb.delete(sb.length()-delete,sb.length());
					}
					sb.append(words[end]);
					this.res.add(sb.toString());
				}
			}
			index=end+1;
		}
        return this.res;
    }	

}
